<?php
/**
 * Index Controller
 *
 * @author     Olivier Parent
 * @copyright  Copyright (c) 2011 Artevelde University College Ghent
 */
class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        $view = $this->view;
        $view->placeholder('baseUrlStyles')->set( $view->baseUrl('styles/') );
    }

    /**
     * Wordt uitgevoerd voordat een Action uitgevoerd wordt.
     */
    public function preDispatch()
    {
        // Als de gebruiker geen identiteit heeft, ga naar loginController indexAction.
        if (!Zend_Auth::getInstance()->hasIdentity()) {
             $this->_helper->redirector('index', 'user'); // Zend_Controller_Action_Helper_Redirector
        }
    }

    public function indexAction()
    {
        $translate = Zend_Registry::get('Zend_Translate');
        $storage = Zend_Auth::getInstance()->getStorage()->read();

        $view = $this->_getView('page_title_home');
        $view->welcome = sprintf( $translate->_('page_message_welcome'), $storage->user_givenname,
                                                                         $storage->user_familyname,
                                                                         $translate->_($storage->user_role));
        $view->goto = $translate->_('link_logout');
    }

    /**
     * @param string $titleTranslationKey
     * @return Zend_View
     */
    private function _getView($titleTranslationKey)
    {
        $translate = Zend_Registry::get('Zend_Translate');

        $view = $this->view;
        $view->title = $translate->_($titleTranslationKey);
        $view->headTitle($view->title);
        $view->headLink()
             ->appendStylesheet($view->placeholder('baseUrlStyles'). 'main.css', 'all');

        return $view;
    }

}
